package ru.st.selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class WorkingOnPagesWithAJAX_1 extends ru.st.selenium.pages.TestBase {
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();

@Test
public void testUntitled () throws Exception {    driver.get(baseUrl + "/chapter1");
    driver.findElement(By.id("loadajax")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.cssSelector("#ajaxdiv > p"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    try {
      assertEquals("The following text has been loaded from another page on this site. It has been loaded in an asynchronous fashion so that we can work through the AJAX section of this chapter", driver.findElement(By.cssSelector("#ajaxdiv > p")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
	}

private boolean isElementPresent (By by) {
  try {
    driver.findElement (by);
    return true;
    } catch (NoSuchElementException e) {
       return false;
    }
  }

private String closeAlertAndGetItText() {
  try {
    Alert alert = driver.switchTo().alert();
    String alertText = alert.getText();
    if (acceptNextAlert) {
      alert.accept();
    } else {
       alert.dismiss();
    } 
    return alertText;
    } finally {
      acceptNextAlert = true;
    }
   }
  }
